package com.xbh.management.controller;

import com.alibaba.fastjson.JSONObject;
import com.nimbusds.jose.JOSEException;
import com.xbh.common.web.core.Result;
import com.xbh.management.exception.JwtSignatureVerifyException;
import com.xbh.management.model.ManMenu;
import com.xbh.management.service.MenuService;
import com.xbh.management.utils.JwtUtilManagement;
import com.xbh.management.utils.TreeNodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.text.ParseException;
import java.util.List;

/**
 * @program: management-center
 * @description:
 * @author: 许宝华
 * @create: 2021-10-01 15:26
 */

@RestController
@RequestMapping(path = "/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;


    /**
     * 查询所有菜单
     * @return
     */
    @PostMapping("/queryAllMenu")
    public Result queryAllMenu(){
        List<ManMenu> manMenus = menuService.queryAllMenu();
        List<ManMenu> menuList = TreeNodeUtil.transformData(manMenus, "0");
        return Result.success(menuList);
    }

    /**
     * 根据id查看菜单信息
     * @param params
     * @return
     */
    @PostMapping("/queryMenuById")
    public Result queryMenuById(@RequestBody JSONObject params){
        Long menuId = params.getLong("menuId");
        ManMenu manMenu = menuService.queryMenuWithMenuId(menuId);
        return Result.success(manMenu);

    }

    /**
     *
     * @param params
     * @return
     * @throws ParseException
     * @throws JOSEException
     * @throws JwtSignatureVerifyException
     */
    @PostMapping("/saveMenu")
    public Result saveMenu(@RequestBody JSONObject params) throws ParseException, JOSEException, JwtSignatureVerifyException {
        ManMenu manMenu = params.toJavaObject(ManMenu.class);
        manMenu.setCreatePerson(JwtUtilManagement.getUsername());
        manMenu.setUpdatePerson(JwtUtilManagement.getUsername());
        int isFlag = menuService.saveMenu(manMenu);
        if(isFlag>0){
            return Result.success(isFlag);
        }
        return Result.failedParams("保存失败");

    }

    /**
     *
     * @param params
     * @return
     */
    @PostMapping("/updateMenu")
    public Result updateMenu(@RequestBody JSONObject params){
        ManMenu manMenu = params.toJavaObject(ManMenu.class);
        int isFlag = menuService.updateMenu(manMenu);
        if(isFlag>0){
            return Result.success(isFlag);

        }
        return Result.failedParams("修改失败");
    }

    /**
     *
     * @param params
     * @return
     */
    @PostMapping("/deleteMenu")
    public Result deleteMenu(@RequestBody JSONObject params){
        String menuIds = params.getString("menuIds");
        int isFlag = menuService.deleteMenuByMenuIds(menuIds);
        if(isFlag>0){
            return Result.success(isFlag);
        }
        return Result.failedParams("删除失败");
    }
}
